<?php
/**
 * Created by PhpStorm.
 * User: Mars.Li
 * Date: 2015/10/13
 * Time: 10:41
 */
namespace Admin\Model;

class VideoAttrModel extends CommonModel {

    public function addAttr($data,$video_id,$type) {
        $datalist = explode(',',$data);
        $list = array();
        foreach ($datalist as $key=> $item) {
            if (!empty($item)){
                $list[$key]['video_id'] = $video_id;
                $list[$key]['attr_id'] = $type;
                $list[$key]['attr_value'] = $item;
            }
        }
        if ($this->addAll($list) === false){
            return false;
        }
        return true;
    }

    public function addOneAttr($data,$video_id,$type) {

        $list['video_id'] = $video_id;
        $list['attr_id'] = $type;
        $list['attr_value'] = $data;

        if ($this->add($list) === false){
            return false;
        }
        return true;
    }

    public function delAttr($video_id,$attr_id) {
        $where['video_id'] = $video_id;
        $where['attr_id'] = $attr_id;
        return $this->where($where)->delete();
    }

    public function delAttrByValue($video_id,$attr_value) {
        $where['video_id'] = $video_id;
        $where['attr_value'] = $attr_value;
        return $this->where($where)->delete();
    }

    public function getAttrByVideoId($video_id,$attr_id) {
        $where['video_id'] = $video_id;
        $where['attr_id'] = $attr_id;
        $data = $this->field('attr_value')->where($where)->select();
        $result = '';
        foreach ($data as $item) {
            $result .= $item['attr_value'].' ';
        }
        return trim($result);
    }



}